package com.derbysoft.lowcostadapter.commons.model;

import com.derbysoft.lowcostadapter.commons.redis.service.RedisSerializable;
import com.derbysoft.lowcostadapter.commons.redis.utils.KeyValueHelper;
import com.derbysoft.lowcostadapter.commons.redis.utils.RedisValueBuilder;

public class RoomStay extends Base implements RedisSerializable {
    public static final String PREFIX = "RM";

    private String propertyID;

    private int propertyRoomTypeID;

    private String start;

    private String end;

    private int minStay;

    private int maxStay;

    private DayType dayType;

    public String getPropertyID() {
        return propertyID;
    }

    public void setPropertyID(String propertyID) {
        this.propertyID = propertyID;
    }

    public int getPropertyRoomTypeID() {
        return propertyRoomTypeID;
    }

    public void setPropertyRoomTypeID(int propertyRoomTypeID) {
        this.propertyRoomTypeID = propertyRoomTypeID;
    }

    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public String getEnd() {
        return end;
    }

    public void setEnd(String end) {
        this.end = end;
    }

    public int getMinStay() {
        return minStay;
    }

    public void setMinStay(int minStay) {
        this.minStay = minStay;
    }

    public int getMaxStay() {
        return maxStay;
    }

    public void setMaxStay(int maxStay) {
        this.maxStay = maxStay;
    }

    public DayType getDayType() {
        return dayType;
    }

    public void setDayType(DayType dayType) {
        this.dayType = dayType;
    }

    public enum DayType {
        ALL_DAYS("All Days"),
        RESTRICTED("Restricted"),
        RESTRICTED_ARRIVAL("Restricted Arrival"),
        RESTRICTED_DEPARTURE("Restricted Departure"),
        SPECIFIC_DAYS("Specific Days"),
        STAYING_ON("Staying On");

        private String description;

        DayType(String description) {
            this.description = description;
        }

        public static DayType fromString(String description) {
            for (DayType dayType : values()) {
                if (dayType.getDescription().equals(description)) {
                    return dayType;
                }
            }
            return null;
        }

        public String getDescription() {
            return description;
        }
    }

    @Override
    public String toKey() {
        return PREFIX + KeyValueHelper.SEPARATOR + getPropertyID() + KeyValueHelper.SEPARATOR + getPropertyRoomTypeID();
    }

    @Override
    public String toFiled() {
        return getStart() + KeyValueHelper.SEPARATOR + getEnd();
    }

    @Override
    public Long expireTime() {
        return null;
    }

    @Override
    public String value() {
        RedisValueBuilder redisValueBuilder = new RedisValueBuilder();
        redisValueBuilder.append(getMinStay());
        redisValueBuilder.append(getMaxStay());
        redisValueBuilder.append(getDayType().getDescription());
        return redisValueBuilder.toString();
    }

    public static RedisSerializable of(String key, String field, String value) {
        RoomStay roomStay = new RoomStay();
        roomStay.setPropertyID(key.split(KeyValueHelper.SEPARATOR)[1]);
        roomStay.setPropertyRoomTypeID(Integer.parseInt(key.split(KeyValueHelper.SEPARATOR)[2]));
        roomStay.setStart(field.split(KeyValueHelper.SEPARATOR)[0]);
        roomStay.setEnd(field.split(KeyValueHelper.SEPARATOR)[1]);

        roomStay.setMinStay(Integer.parseInt(value.split(KeyValueHelper.SEPARATOR)[0]));
        roomStay.setMaxStay(Integer.parseInt(value.split(KeyValueHelper.SEPARATOR)[1]));
        roomStay.setDayType(RoomStay.DayType.fromString(value.split(KeyValueHelper.SEPARATOR)[2]));
        return roomStay;
    }
}
